﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Services
{
	using System.Collections.Generic;
	using Falcon.Core.Domain;
	using Falcon.Core.Domain.Repositories;
	using Falcon.Core.Domain.Services;
	using Falcon.Core.Domain.Values;
	using Falcon.Core.Extensions;

	public class UserMessageFolderService : IUserMessageFolderService
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="UserMessageFolderService"/> class.
		/// </summary>
		/// <param name="identityService">The identity service.</param>
		/// <param name="userMessageFolderRepository">The user message folder repository.</param>
		public UserMessageFolderService(
			IIdentityService identityService, 
			IUserMessageFolderRepository userMessageFolderRepository)
		{
			this.IdentityService = identityService;
			this.UserMessageFolderRepository = userMessageFolderRepository;
		}

		/// <summary>
		/// Gets or sets the identity service.
		/// </summary>
		/// <value>The identity service.</value>
		public IIdentityService IdentityService
		{
			get;
			private set;
		}

		/// <summary>
		/// Gets or sets the user message folder repository.
		/// </summary>
		/// <value>The user message folder repository.</value>
		public IUserMessageFolderRepository UserMessageFolderRepository
		{
			get; 
			private set;
		}

		/// <summary>
		/// Gets the user message folder by the specified specification.
		/// </summary>
		/// <param name="specification">The <see cref="UserMessageFolderSpecification"/>.</param>
		/// <returns></returns>
		public UserMessageFolder GetBy(UserMessageFolderSpecification specification)
		{
			return this.UserMessageFolderRepository.GetBy(specification);
		}

		/// <summary>
		/// Gets the user message folder by id.
		/// </summary>
		/// <param name="id">The id.</param>
		/// <returns></returns>
		public UserMessageFolder GetById(int id)
		{
			if (id == 0)
			{
				return this.Inbox();
			}

			return this.UserMessageFolderRepository.GetById(id);
		}

		/// <summary>
		/// Gets all the user message folders.
		/// </summary>
		/// <returns></returns>
		public IList<UserMessageFolder> GetAll()
		{
			return this.UserMessageFolderRepository.GetAll();
		}

		/// <summary>
		/// Gets all the user message folders.
		/// </summary>
		/// <param name="specification">The <see cref="UserMessageFolderSpecification"/>.</param>
		/// <returns></returns>
		public IList<UserMessageFolder> GetAll(UserMessageFolderSpecification specification)
		{
			return this.UserMessageFolderRepository.GetAll(specification);
		}

		/// <summary>
		/// Gets the inbox folder.
		/// </summary>
		/// <returns></returns>
		public UserMessageFolder Inbox()
		{
			var folder = new UserMessageFolder
			{
				Id = 0,
				Title = UserMessageFolder.Inbox.UppercaseFirst()
			};

			folder.Slug = folder.Title.Slug();

			return folder;
		}

		/// <summary>
		/// Creates the user message.
		/// </summary>
		/// <returns></returns>
		public UserMessageFolder Create()
		{
			return new UserMessageFolder();
		}

		/// <summary>
		/// Inserts the user message folder.
		/// </summary>
		/// <param name="folder">The <see cref="UserMessageFolder"/>.</param>
		public void Insert(UserMessageFolder folder)
		{
			folder.Validate();

			folder.Slug = folder.Title.Slug();

			this.UserMessageFolderRepository.Insert(folder);
		}

		/// <summary>
		/// Inserts the user message folder.
		/// </summary>
		/// <param name="folder">The folder.</param>
		/// <param name="value">The value.</param>
		public void Insert(UserMessageFolder folder, UserMessageFolderCreate value)
		{
			value.Validate();

			var identity = IdentityService.Current;

			folder.UserId = identity.Id;
			folder.Title = value.Title;

			this.Insert(folder);
		}

		/// <summary>
		/// Updates the user message folder.
		/// </summary>
		/// <param name="folder">The <see cref="UserMessageFolder"/>.</param>
		public void Update(UserMessageFolder folder)
		{
			folder.Validate();

			folder.Slug = folder.Title.Slug();

			this.UserMessageFolderRepository.Update(folder);
		}

		/// <summary>
		/// Updates the user message folder.
		/// </summary>
		/// <param name="folder">The folder.</param>
		/// <param name="value">The value.</param>
		public void Update(UserMessageFolder folder, UserMessageFolderUpdate value)
		{
			value.Validate();

			folder.Title = value.Title;

			this.Update(folder);
		}

		/// <summary>
		/// Deletes the user message folder.
		/// </summary>
		/// <param name="folder">The <see cref="UserMessageFolder"/>.</param>
		public void Delete(UserMessageFolder folder)
		{
			this.UserMessageFolderRepository.Delete(folder);
		}
	}
}